package com.mdd.admin.service.system.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdd.admin.service.system.ISystemSignInConfigService;
import com.mdd.admin.validate.system.SystemSignInConfingParam;
import com.mdd.common.utils.LsConfigUtil;
import com.mdd.common.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Comparator;

@Service
public class SystemSignInConfigServiceImpl implements ISystemSignInConfigService {

    /**
     * 新增或更新 积分签到规则
     *
     * @author panweiliang
     * @param systemSignInConfingParam 参数
     */
    @Override
    public void addOrUpdateSignInConfig(SystemSignInConfingParam systemSignInConfingParam) {
        LsConfigUtil.set("sign-in_points", "enable", String.valueOf(systemSignInConfingParam.getEnable()));
        LsConfigUtil.set("sign-in_points", "changeless", String.valueOf(systemSignInConfingParam.getChangeless()));
        String rewardRulesJson = String.valueOf(systemSignInConfingParam.getReward_rules_json());
        if (StringUtil.isNotEmpty(rewardRulesJson)) {
            JSONArray jsonArray = JSONArray.parseArray(rewardRulesJson);
            jsonArray.sort(Comparator.comparing(st->((JSONObject)st).getInteger("sort")).reversed());
            LsConfigUtil.set("sign-in_points", "reward_rules_json", jsonArray.toString());
        }
        LsConfigUtil.set("sign-in_points", "rule_description", String.valueOf(systemSignInConfingParam.getRule_description()));
    }
}
